package com.example.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.AutoLog;
import com.example.common.Result;
import com.example.domain.entity.Notice;
import com.example.domain.entity.Params;
import com.example.domain.entity.Reserve;
import com.example.domain.entity.Room;
import com.example.service.NoticeService;
import com.example.service.ReserveService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@CrossOrigin
@RestController
@RequestMapping("/reserve")
public class ReserveController {
    
    private static final Logger log = LoggerFactory.getLogger(ReserveController.class);
    
//    @Autowired
//    private ReserveServiceA reserveServiceA;

    @Autowired
    private ReserveService reserveService;

    @Autowired
    private NoticeService noticeService;
    
    @GetMapping("/findAllNotice")
    public Result findAllNotice(Params params) {
        List<Notice> list = noticeService.findAll(params);
        return Result.success(list);
    }
    
    @GetMapping("/findAll")
    public Result findAll() {
        List<Reserve> list = reserveService.findAll();
        return Result.success(list);
    }
    
    @PostMapping
    @AutoLog("预约自习室")
    public Result save(@RequestBody Reserve reserve) {
        return reserveService.add(reserve);
    }
    
    @PostMapping("/update1")//员工点击通过按钮修改状态（通过取消预约）  已修改
    public Result update1(@RequestBody Reserve reserve) {
        reserveService.update1(reserve);
        return Result.success();
    }
    
    @PostMapping("/update2")//管理员点击不通过按钮修改状态  改为：用户点击确认操作，会议室进入空闲状态
    public Result update2(@RequestBody Reserve reserve) {
        reserveService.update2(reserve);
        return Result.success();
    }
    
    @PostMapping("/update3")//管理员或者学生点击结束使用按钮修改状态
    public Result update3(@RequestBody Reserve reserve) {
        reserveService.update3(reserve);
        return Result.success();
    }
    
    @PostMapping("/update4")//用户点击取消预约按钮修改状态 已修改
    public Result update4(@RequestBody Reserve reserve) {
        reserveService.update4(reserve);
        return Result.success();
    }

    @PostMapping("/confirm")//用户点击确认取消预约  room置为空闲
    public Result confirm(@RequestBody Reserve reserve) {
        reserveService.confirm(reserve);
        return Result.success();
    }
    
    @GetMapping("/search")
    public Result findBySearch(Params params) {
        log.info("拦截器已放行，正式调用接口内部，查询自习室预约记录信息");
        Page<Reserve> info = reserveService.findBySearch(params);
        return Result.success(info);
    }
    
    @GetMapping("/echarts/bar")
    public Result bar() {
        // 查询出所有自习室预约记录
        List<Reserve> list = reserveService.findAll();
        Map<String, Long> collect = list.stream()
                .filter(x -> ObjectUtil.isNotEmpty(x.getName()))
                .collect(Collectors.groupingBy(Reserve::getName, Collectors.counting()));
        
        List<String> xAxis = new ArrayList<>();
        List<Long> yAxis = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(collect)) {
            for (String key : collect.keySet()) {
                xAxis.add(key);
                yAxis.add(collect.get(key));
            }
        }
        
        Map<String, Object> map = new HashMap<>();
        map.put("xAxis", xAxis);
        map.put("yAxis", yAxis);
        
        return Result.success(map);
    }
    
    /*@DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        reserveService.delete(id);
        return Result.success();
    }*/
}
